package br.edu.universo.si.projeto2.infojob.client.componentes;

import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * COMPONENTE - Campo para CPF
 * @author Andrey A. de Oliveira
 * @since 21/09/2008
 */
public class CPFBox extends TextBox
{
	public CPFBox()
	{
		super();
		
		this.setWidth("120px");
		this.setTextAlignment(ALIGN_RIGHT);
		this.setMaxLength(14);
		
		this.addKeyboardListener(
			new KeyboardListener()
			{
				public void onKeyDown(Widget sender, char keyCode, int modifiers)
				{
					if (keyCode != '0' && keyCode != '1' && keyCode != '2' && keyCode != '3' && keyCode != '4' && 
						keyCode != '5' && keyCode != '6' && keyCode != '7' && keyCode != '8' && keyCode != '9' &&
						((int) keyCode) != KEY_BACKSPACE && ((int) keyCode) != KEY_DELETE && ((int) keyCode) != KEY_HOME && ((int) keyCode) != KEY_END 
						&& ((int) keyCode) != KEY_TAB && ((int) keyCode) != KEY_LEFT && ((int) keyCode) != KEY_RIGHT && ((int) keyCode) != KEY_SHIFT)
					{
						((TextBox) sender).cancelKey();
					}
				}

				public void onKeyPress(Widget sender, char keyCode, int modifiers)
				{
					if (keyCode != (char) KEY_BACKSPACE)
					{
						if (CPFBox.this.getText().length() == 3
								|| CPFBox.this.getText().length() == 7)
						{
							CPFBox.this.setText(CPFBox.this.getText() + ".");
						}
						
						if (CPFBox.this.getText().length() == 11)
						{
							CPFBox.this.setText(CPFBox.this.getText() + "-");
						}
					}
				};

				// implementacao vazia...
				public void onKeyUp(Widget sender, char keyCode, int modifiers) { };
			}
		);
		
		// se o usuario nao inserir o CPF completo, apaga o conteudo do campo de texto...
		this.addFocusListener(
			new FocusListener()
			{
				public void onLostFocus(Widget sender)
				{
					if (CPFBox.this.getText().length() != 14)
					{
						CPFBox.this.setText("");
					}
				}

				public void onFocus(Widget sender) { };
			}
		);
		
	}

    private static String calcDigVerif(String num)
    {
        Integer primDig, segDig;
        int soma = 0, peso = 10;

        for (int i = 0; i < num.length(); i++)
        {
        	soma += Integer.parseInt(num.substring(i, i + 1)) * peso--;
        }

        if (soma % 11 == 0 | soma % 11 == 1)
        {
        	primDig = new Integer(0);
        }
        else
        {
        	primDig = new Integer(11 - (soma % 11));
        }

        soma = 0;
        peso = 11;

        for (int i = 0; i < num.length(); i++)
        {
        	soma += Integer.parseInt(num.substring(i, i + 1)) * peso--;
        }

        soma += primDig.intValue() * 2;
        
        if (soma % 11 == 0 | soma % 11 == 1)
        {
        	
            segDig = new Integer(0);
        }
        else
        {
            segDig = new Integer(11 - (soma % 11));
        }


        return primDig.toString() + segDig.toString();
    }

    private static int calcSegDig(String cpf, int primDig)
    {
        int soma = 0, peso = 11;

        for (int i = 0; i < cpf.length(); i++)
        {
        	soma += Integer.parseInt(cpf.substring(i, i + 1)) * peso--;
        }

        soma += primDig * 2;

        if (soma % 11 == 0 | soma % 11 == 1)
        {
        	return 0;
        }
        else
        {
        	return 11 - (soma % 11);
        }
    }
    
    public boolean isVazio()
    {
    	if (CPFBox.this.getText().equalsIgnoreCase(""))
    	{
    		return true;
    	}
    	
    	return false;
    }

    public boolean isCPFValido()
    {
    	// limpa o CPF - retira o ponto e o hifen
    	String CPF = CPFBox.this.getText().replace(".", "");
    	CPF = CPF.replace("-", "");

        if (CPF.length() != 11)
        {
            return false;
        }

        String numDig = CPF.substring(0, 9);
        return calcDigVerif(numDig).equals(CPF.substring(9, 11));
    }
}
